﻿/*  FastExplorer
    Copyright (C) 2008 Alexandr Yavorsky

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/* class Dir */

function Dir(_location)
{
	this.location = _location;

	if (this.location != "" && this.location.charAt(this.location.length-1) != "\\")
		this.location += "\\";

	this.elements = new Array();

	this.type = "none";

	this.refresh();
}

Dir.prototype.fs = new ActiveXObject("Scripting.FileSystemObject");
Dir.prototype.shell = new ActiveXObject("Shell.Application");
Dir.prototype.sshell = new ActiveXObject("WScript.Shell");

Dir.prototype.refresh = function ()
{
	if (this.location == "")
	{
		this.elements = new Array();
	
		fc = new Enumerator(fs.Drives);

		for (;!fc.atEnd(); fc.moveNext())
		{
			var subtype = "";
			
			if (fc.item().DriveType == 4)
				subtype = "_cd";
			else if (fc.item().DriveType == 3)
				subtype = "_network";
			else if (fc.item().DriveType == 1)
				subtype = "_removable";

			this.elements.push({
				"type": "drive" + subtype, 
				"name": fc.item().DriveLetter + ":", 
				"volume_name": subtype=="" ? fc.item().VolumeName : "",
				"file_size": subtype=="" ? Math.floor(fc.item().TotalSize/1000000) : 0,
				"free_space": subtype=="" ? Math.floor(fc.item().FreeSpace/1000000) : 0
			}); 
		}
		this.type = "drives";
	}
	else
	{
		this.elements = new Array();
		this.elements.push({"type": "up", "name": "..", "volume_name": "", "file_size": "" });
		try 
		{
			var d = fs.GetFolder(this.location);
		
			fc = new Enumerator(d.SubFolders);
			
			for (;!fc.atEnd(); fc.moveNext())
				this.elements.push({
					"type": "folder", 
					"name": fc.item().Name, 
					"volume_name": "",
					"file_size": ""
				});

			fc = new Enumerator(d.Files);

			for (;!fc.atEnd(); fc.moveNext())
				this.elements.push({
					"type": "file", 
					"name": fc.item().Name, 
					"volume_name": "",
					"file_size": Math.floor(fc.item().Size/1000000)
				});
			
			this.type = "folder";
		}
		catch(e)
		{
		}

	}
}

Dir.prototype.upper = function()
{
	var newloc = this.location.substr(0, this.location.length-1);

	var ind = newloc.lastIndexOf("\\");

	if (ind == -1)
		newloc = ""
	else 
		newloc = newloc.substring(0, ind+1);

	this.location = newloc;
}

Dir.prototype.run = function(fn)
{
  try
  {
    this.sshell.Run("\"" + fn + "\"");
      return true;
  }
  catch(e)
  {
    return false;
  }
}

Dir.prototype.navigate = function (id)
{
	var elem = this.elements[id];

	if (elem.type == "file")
	{
		this.run(this.location + elem.name);
	}
	else 
	if (elem.type == "folder")
	{
		this.location += elem.name + "\\";
		this.refresh();
	}
	else 
	if (elem.type == "up")
	{
		this.upper();
		this.refresh();
	}
	else 
	if (elem.type.indexOf("drive") != -1)
	{
		this.location = elem.name + "\\";
		this.refresh();
	}
}

Dir.prototype.openInShell = function ()
{
	this.shell.Open(this.location);
}
